//
//  YXSetViewController.m
//  跑跑快销
//
//  Created by 杨潇 on 14-4-28.
//  Copyright (c) 2014年 ylife. All rights reserved.
//

#import "YXSetViewController.h"
#import "YXBluetoothSetViewController.h"
#import "YXFeedbackViewController.h"
#import "YXIntroduceViewController.h"
#import "YXAboutViewController.h"

@interface YXSetViewController ()

@end

@implementation YXSetViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    
    NSArray * titleArr1 = [NSArray arrayWithObjects:@"消息提示音",@"蓝牙打印机设置", nil];
    NSArray * titleArr2 = [NSArray arrayWithObjects:@"短信分享跑跑快销",@"微信分享跑跑快销",@"分享到微信朋友圈", nil];
    NSArray * titleArr3 = [NSArray arrayWithObjects:@"反馈",@"功能介绍",@"关于", nil];
    _titleArr = [[NSArray alloc] initWithObjects:titleArr1,titleArr2,titleArr3, nil];
    
    YXSetView * setView = [[YXSetView alloc] init];
    self.view = setView;
    
    setView.tableVi.delegate = self;
    setView.tableVi.dataSource = self;
}

- (void)sendReqToWXWithScence:(int)scence
{
    if ([WXApi isWXAppInstalled] && [WXApi isWXAppSupportApi]) {
        WXMediaMessage * message = [WXMediaMessage message];
        message.title = @"微信分享跑跑快销给好友";
        message.description =@"赶快下载跑跑快销，给力的销售管理工具，有它销售更得力！http://app.ylife.cn/003774.apk";
        WXImageObject * image = [WXImageObject object];
        //        image.imageData =
        message.mediaObject = image;
        SendMessageToWXReq * req = [[SendMessageToWXReq alloc] init];
        req.bText = NO;
        req.scene = scence;
        req.message = message;
        
        [WXApi sendReq:req];
    }else{
        UIAlertView *alView = [[UIAlertView alloc]initWithTitle:@"" message:@"你的iPhone上还没有安装微信,无法使用此功能，使用微信可以方便的把你喜欢的作品分享给好友。" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"免费下载微信", nil];
        [alView show];
    }
}

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        NSString *weiXinLink = @"itms-apps://itunes.apple.com/cn/app/wei-xin/id414478124?mt=8";
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:weiXinLink]];
    }
}

#pragma mark - UITableViewDataSource,UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.section) {
        case 0:
        {
            if (indexPath.row == 1) {
                YXBluetoothSetViewController * bluetoothVC = [[YXBluetoothSetViewController alloc] init];
                bluetoothVC.title = @"蓝牙设置";
                [self setHidesBottomBarWhenPushed:YES];
                [self.navigationController pushViewController:bluetoothVC animated:YES];
            }
        }
            break;
        case 1:
        {
            switch (indexPath.row) {
                case 0:
                {
                    if ([MFMessageComposeViewController canSendText]) {
                        MFMessageComposeViewController * messageVC = [[MFMessageComposeViewController alloc] init];
                        messageVC.body = @"赶快下载跑跑快销，给力的销售管理工具，有它销售更得力！http://app.ylife.cn/003774.apk";
                        messageVC.messageComposeDelegate = self;
                        [self presentViewController:messageVC animated:YES completion:nil];
                    }else{
                        [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:@"您的设备不支持短信服务"];
                    }
                }
                    break;
                case 1:
                {
                    [self sendReqToWXWithScence:WXSceneSession];
                }
                    break;
                case 2:
                {
                    [self sendReqToWXWithScence:WXSceneTimeline];
                }
                    break;
                    
                default:
                    break;
            }
        }
            break;
        case 2:
        {
            switch (indexPath.row) {
                case 0:
                {
                    YXFeedbackViewController * feedbackVC = [[YXFeedbackViewController alloc] init];
                    feedbackVC.title = @"反馈";
                    [self setHidesBottomBarWhenPushed:YES];
                    [self.navigationController pushViewController:feedbackVC animated:YES];
                }
                    break;
                case 1:
                {
                    YXIntroduceViewController * introduceVC = [[YXIntroduceViewController alloc] init];
                    introduceVC.title = @"介绍";
                    [self setHidesBottomBarWhenPushed:YES];
                    [self.navigationController pushViewController:introduceVC animated:YES];
                }
                    break;
                case 2:
                {
                    YXAboutViewController * aboutVC = [[YXAboutViewController alloc] init];
                    [self setHidesBottomBarWhenPushed:YES];
                    [self.navigationController pushViewController:aboutVC animated:YES];
                }
                    break;
                    
                default:
                    break;
            }
        }
            break;
            
        default:
            break;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [_titleArr count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[_titleArr objectAtIndex:section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * identifier = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if (indexPath.section == 0 && indexPath.row == 0) {
        UISwitch * swit = [[UISwitch alloc] initWithFrame:CGRectMake(250, 6, 60, 30)];
        [swit addTarget:self action:@selector(touchSwitch:) forControlEvents:UIControlEventValueChanged];
        swit.on = YES;
        [cell addSubview:swit];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    cell.textLabel.text = [[_titleArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 5;
}

#pragma mark - MFMessageComposeViewControllerDelegate
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    if (result == MessageComposeResultFailed) {
        [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:@"短信发送失败。"];
    }
    [controller dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - BtnsClick
- (void)touchSwitch:(UISwitch *)swit
{
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
